package com.xjh.serverinspect.security;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * <p>
 * jwt持有者
 * </p>
 *
 * @author xujinghui
 * @since 2024-12-12
 */
@Component
public class JwtHolder {

    @Value("${jwt.secret:dZSbAZ3du0ywZhl2cnrbi87eJSgmIoQYISQlL/S429g=}")
    private String secret;

    @Value("${jwt.expire-millis:7200000}")
    private long expireMillis; // 2小时

    @Value("${jwt.refresh-expire-millis:2592000000}")
    private long refreshExpireMillis = 2592000000L; // 30天

    /**
     * 创建token
     *
     * @param claims token中的内容
     * @return String
     */
    public String createAccessToken(Map<String, Object> claims) {
        return JWTUtils.create(claims, secret, expireMillis);
    }

    /**
     * 创建token
     *
     * @param claims token中的内容
     * @return String
     */
    public String createRefreshToken(Map<String, Object> claims) {
        claims.put("flag", "refresh_token");
        return JWTUtils.create(claims, secret, refreshExpireMillis);
    }

    /**
     * 解析token
     *
     * @param token token
     * @return Map<String, Object>
     */
    public Map<String, Object> parseToken(String token) {
        return JWTUtils.silentParse(token, secret);
    }

}
